<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\adminapi\controller\translation;

use app\adminapi\controller\Base;
use app\common\model\Language as LanguageModel;

/**
 * 语言管理控制器
 */
class Language extends Base
{
    /**
     * @notes 获取语言列表
     * @auth true
     */
    public function getList()
    {
        $list = LanguageModel::order('sort_order', 'asc')->select();
        $this->success('', $list);
    }

    /**
     * @notes 获取启用的语言列表
     * @auth true
     */
    public function getEnabledList()
    {
        $list = LanguageModel::getEnabledLanguages();
        $this->success('', $list);
    }

    /**
     * @notes 添加语言
     * @auth true
     */
    public function addLanguage()
    {
        $code       = inputs('code/s', '');
        $name       = inputs('name/s', '');
        $nativeName = inputs('native_name/s', '');
        $sortOrder  = inputs('sort_order/d', 0);

        if (!$code || !$name || !$nativeName) {
            $this->error('参数不完整');
        }

        // 检查语言代码是否已存在
        if (LanguageModel::where('code', $code)->count() > 0) {
            $this->error('语言代码已存在');
        }

        $language = new LanguageModel();
        $language->save([
            'code'        => $code,
            'name'        => $name,
            'native_name' => $nativeName,
            'sort_order'  => $sortOrder,
            'is_enabled'  => 1,
        ]);

        $this->success('添加成功');
    }

    /**
     * @notes 编辑语言
     * @auth true
     */
    public function editLanguage()
    {
        $id         = inputs('id/d', 0);
        $name       = inputs('name/s', '');
        $nativeName = inputs('native_name/s', '');
        $sortOrder  = inputs('sort_order/d', 0);

        $language = LanguageModel::find($id);
        if (!$language) {
            $this->error('语言不存在');
        }

        $language->save([
            'name'        => $name,
            'native_name' => $nativeName,
            'sort_order'  => $sortOrder,
        ]);

        $this->success('编辑成功');
    }

    /**
     * @notes 删除语言
     * @auth true
     */
    public function deleteLanguage()
    {
        $id       = inputs('id/d', 0);
        $language = LanguageModel::find($id);

        if (!$language) {
            $this->error('语言不存在');
        }

        if ($language->is_default) {
            $this->error('不能删除默认语言');
        }

        $language->delete();
        $this->success('删除成功');
    }

    /**
     * @notes 启用/禁用语言
     * @auth true
     */
    public function toggleLanguage()
    {
        $id       = inputs('id/d', 0);
        $language = LanguageModel::find($id);

        if (!$language) {
            $this->error('语言不存在');
        }

        if ($language->is_default && $language->is_enabled) {
            $this->error('不能禁用默认语言');
        }

        $language->is_enabled = $language->is_enabled ? 0 : 1;
        $language->save();

        $action = $language->is_enabled ? '启用' : '禁用';
        $this->success("{$action}成功");
    }

    /**
     * @notes 设置默认语言
     * @auth true
     */
    public function setDefault()
    {
        $id       = inputs('id/d', 0);
        $language = LanguageModel::find($id);

        if (!$language) {
            $this->error('语言不存在');
        }

        if (!$language->is_enabled) {
            $this->error('请先启用该语言');
        }

        // 取消其他语言的默认状态
        LanguageModel::where('is_default', 1)->update(['is_default' => 0]);

        // 设置新的默认语言
        $language->is_default = 1;
        $language->save();

        $this->success('设置默认语言成功');
    }

    /**
     * @notes 更新语言排序
     * @auth true
     */
    public function updateSort()
    {
        $data = inputs('data/a', []);

        foreach ($data as $item) {
            if (isset($item['id']) && isset($item['sort_order'])) {
                LanguageModel::where('id', $item['id'])->update([
                    'sort_order' => $item['sort_order']
                ]);
            }
        }

        $this->success('排序更新成功');
    }

    /**
     * @notes 获取语言选项
     * @auth true
     */
    public function getLanguageOptions()
    {
        $options = LanguageModel::getLanguageOptions();
        $this->success('', $options);
    }

    /**
     * @notes 获取推荐的语言列表
     * @auth true
     */
    public function getRecommendedLanguages()
    {
        $recommended = [
            ['code' => 'zh_CN', 'name' => '简体中文', 'native_name' => '简体中文'],
            ['code' => 'zh_TW', 'name' => '繁体中文', 'native_name' => '繁體中文'],
            ['code' => 'en_US', 'name' => 'English', 'native_name' => 'English'],
            ['code' => 'ja_JP', 'name' => '日本語', 'native_name' => '日本語'],
            ['code' => 'ko_KR', 'name' => '한국어', 'native_name' => '한국어'],
            ['code' => 'fr_FR', 'name' => 'Français', 'native_name' => 'Français'],
            ['code' => 'de_DE', 'name' => 'Deutsch', 'native_name' => 'Deutsch'],
            ['code' => 'es_ES', 'name' => 'Español', 'native_name' => 'Español'],
            ['code' => 'pt_PT', 'name' => 'Português', 'native_name' => 'Português'],
            ['code' => 'ru_RU', 'name' => 'Русский', 'native_name' => 'Русский'],
            ['code' => 'ar_SA', 'name' => 'العربية', 'native_name' => 'العربية'],
            ['code' => 'th_TH', 'name' => 'ไทย', 'native_name' => 'ไทย'],
            ['code' => 'vi_VN', 'name' => 'Tiếng Việt', 'native_name' => 'Tiếng Việt'],
            ['code' => 'id_ID', 'name' => 'Bahasa Indonesia', 'native_name' => 'Bahasa Indonesia'],
            ['code' => 'ms_MY', 'name' => 'Bahasa Melayu', 'native_name' => 'Bahasa Melayu'],
        ];

        $this->success('', $recommended);
    }
}
